﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ServBus.ListForm2010.Setup.Utilities;

namespace ServBus.ListForm2010.Setup.Commands
{
    class IisResetCmd : BaseCommand
    {
        public override string Description
        {
            get { return Properties.Resources.CmdIisReset; }
        }

        public override bool AllowRollback
        {
            get
            {
                return false;
            }
        }

        public override bool Execute()
        {
            try
            {
                Log.WriteLog(Log.LogType.Info, "Start Reseting IIS...");
                Process process = new Process();
                process.StartInfo = new ProcessStartInfo("iisreset");
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                if(!process.WaitForExit(30000))
                {
                    throw new Exception("Timeout.");
                }
                else if(process.ExitCode != 0)
                {
                    throw new Exception("Exit with Error Code " + process.ExitCode.ToString());
                }
                Log.WriteLog(Log.LogType.Info, "IIS Reseted.");
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Log.LogType.Error, "IIS Reset Error. " + ex.GetType().FullName + ". " + ex.Message);
                return false;
            }
        }

        public override bool Rollback()
        {
            return true;
        }
    }
}
